//
//  DSKeyboard.m
//  ANYpocket
//
//  Created by wkun on 2019/10/1.
//  Copyright © 2019 bill. All rights reserved.
//

#import "DSKeyboard.h"
#import <UIKit/UIKit.h>

@implementation DSKeyboard{
    BOOL _keyBoardIsVisible;//键盘是否已经弹出
}

@synthesize keyBoardHeight = _keyBoardHeight;
@synthesize isShowingKeyboard = _keyBoardIsVisible;

- (id)init{
   self = [super init];
    if( self){
        _keyBoardIsVisible = NO;
        _keyBoardHeight = -1;
    }
    return self;
}

#pragma mark - Notification

- (void)keyBoardDidShow{
    //键盘弹出完毕
    if( _delegate && [_delegate respondsToSelector:@selector(keyBoardDidShow:)] ){
        [_delegate keyBoardDidShow:self];
    }
}

-(void)keyBoardWillShow:(NSNotification*)noti{
    
    _keyBoardIsVisible = YES;
 
    NSDictionary *userInfo = [noti userInfo];
    //得到键盘弹出后，键盘视图的Y坐标
    NSLog(@"_keyBoardHeight=%lf",_keyBoardHeight);
    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    _keyBoardHeight = value.CGRectValue.size.height;
    if( self.delegate && [_delegate respondsToSelector:@selector(keyBoardWillShow:keyBoardHeight:)] && _keyBoardHeight> 100){
        
        [_delegate keyBoardWillShow:self keyBoardHeight:_keyBoardHeight];
    }
}

-(void)keyboardWillHide:(NSNotification*)noti{
    
    _keyBoardIsVisible = NO;
    
    if( _delegate && [_delegate respondsToSelector:@selector(keyBoardWillHide:keyBoardHeight:)] ){
        [_delegate keyBoardWillHide:self keyBoardHeight:_keyBoardHeight];
    }
}

#pragma mark - public

-(void)addObserverKeyBoard{

    NSNotificationCenter *noti = [NSNotificationCenter defaultCenter];
    [noti addObserver:self selector:@selector(keyBoardWillShow:)  name:UIKeyboardWillShowNotification object:nil];
    [noti addObserver:self selector:@selector(keyBoardDidShow) name:UIKeyboardDidShowNotification object:nil];
    [noti addObserver:self selector:@selector(keyboardWillHide:)  name:UIKeyboardWillHideNotification object:nil];
}

-(void)removeObserver{

    NSNotificationCenter *noti = [NSNotificationCenter defaultCenter];
    [noti removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [noti removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [noti removeObserver:self name:UIKeyboardDidShowNotification object:nil];
}


@end
